package com.codi.bus.core.dao.impl;

import com.codi.base.dao.BaseDAOImpl;
import com.codi.bus.core.dao.UserBankDao;
import com.codi.bus.core.domain.UserBank;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository("userBankDao")
public class UserBankDaoImpl extends BaseDAOImpl<UserBank> implements UserBankDao {

    @Override
    public boolean updateBank(UserBank bank) {
        return this.update(UserBank.class.getName() + ".updateByPrimaryKeySelective", bank) == 0;
    }

    @Override
    public void add(UserBank entity) {
        entity.setCreateDate(new Date());
        entity.setIsValid(true);
        this.insert(entity);
    }


    @Override
    public boolean isExistBankNo(String bankNo, String capitalMode) {
        UserBank entity = new UserBank();
        entity.setUserBankNo(bankNo);
        entity.setCapitalMode(capitalMode);
        entity = this.getObject(UserBank.class.getName() + ".selectByBankNo", entity);
        if (entity == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public UserBank getEntityByBankNo(Long userId, String bankNo, String capitalMode) {
        UserBank query = new UserBank();
        query.setUserBankNo(bankNo);
        query.setUserId(userId);
        query.setCapitalMode(capitalMode);
        return this.getObject(UserBank.class.getName() + ".selectByBankNoAndUserId", query);
    }

    @Override
    public List<UserBank> getListByUserId(Long userId) {
        return this.findList(UserBank.class.getName() + ".selectByUserId", userId);
    }

    @Override
    public List<UserBank> getBankCardsNotInCounter(Long userId) {
        return this.findList(UserBank.class.getName() + ".getBankCardsNotInCounter", userId);
    }

    @Override
    public UserBank getByTradeAccount(String tradeAccount) {

        Preconditions.checkNotNull(tradeAccount, "交易账号不能为空");
        tradeAccount = tradeAccount.trim();

        return this.getObject(UserBank.class.getName() + ".getByTradeAccount", tradeAccount);
    }

    @Override
    public UserBank get(UserBank record) {
        return this.getObject(UserBank.class.getName() + ".get", record);
    }

    @Override
    public List<UserBank> getBanks(UserBank record) {
        return this.findList(UserBank.class.getName() + ".get", record);
    }
}
